﻿using System;
using System.Linq;
using PTop.Code;
using PTop.Domain.Entity.Questionnaire;
using PTop.Domain.IRepository.Questionnaire;
using PTop.Repository.Questionnaire;
using System.Collections.Generic;

namespace PTop.Application.Questionnaire
{
    /// <summary>
    /// 调查问卷App
    /// </summary>	
    public class QnPaperApp
    {
        private IQnPaperRepository service = new QnPaperRepository();

        public List<QnPaperEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<QnPaperEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_Title.Contains(keyword));
            }
            return service.FindList(expression, pagination);
        }

        public List<QnPaperEntity> GetUnreleaseList(string keyword)
        {
            var today = DateTime.Now.Date;
            var expression = ExtLinq.True<QnPaperEntity>()
                .And(m => m.F_State == 0)
                .And(m => m.F_ValidTime >= today);
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_Title.Contains(keyword));
            }
            return service.IQueryable(expression).OrderByDescending(t => t.F_CreatorTime).ToList();
        }

        public QnPaperEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public void Delete(string keyValue)
        {
            new QnQuestionApp().DeleteByPaperId(keyValue);
            new QnAnswerApp().DeleteByPaperId(keyValue);
            service.Delete(t => t.F_Id == keyValue);
        }

        public void SubmitForm(QnPaperEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Modify(keyValue);
                service.Update(entity);
            }
            else
            {
                entity.F_ValidTime = entity.F_ValidTime.Date;
                entity.Create();
                //entity.F_LastModifyTime = entity.F_CreatorTime;
                //entity.F_LastModifyUserId = entity.F_CreatorUserId;
                service.Insert(entity);
            }
        }

    }
}